/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import java.awt.event.*;
/**
 *
 * @author 
 */

public class ImageLoader {
    public static void main(String[] args) {
        LoaderFrame f = new LoaderFrame();
        f.setVisible(true);
    }
}

/** Wczytuje okno */
class LoaderFrame extends JFrame {

    private JTextField textField;
    private JProgressBar progressBar;
    private JLabel imageLabel;
    private static final String SAMPLE_URL = "http://environment.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Environment/Images/Interactive_GW/kantegapeak-10120779-sw.jpg";

    public LoaderFrame() {
        super("SwingWorker w akcji;)");
        setSize(700,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initGUI();
    }

    private void initGUI() {
        textField = new JTextField(20);
        textField.setText(SAMPLE_URL);
        progressBar = new JProgressBar();
        imageLabel = new JLabel();


        JPanel centerPanel = new JPanel();
        centerPanel.add(imageLabel, BorderLayout.CENTER);
        add(centerPanel, BorderLayout.CENTER);

        JPanel downPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5,5));
        downPanel.add(progressBar);
        downPanel.add(new JLabel("Podaj url:"));
        downPanel.add(textField);
        JButton button = new JButton("Pokaż");
        button.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e) {
               SwingWorker<Icon, Void> worker = new SwingWorker<Icon, Void>(){

                    @Override
                    protected Icon doInBackground() throws Exception {
                        progressBar.setIndeterminate(true);
                        return loadImage(textField.getText());
                    }

                    @Override
                    protected void done() {
                        try {
                            imageLabel.setIcon(get());
                            progressBar.setIndeterminate(false);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

               };
               worker.execute();
           }
        });
        downPanel.add(button);
        add(downPanel, BorderLayout.SOUTH);
    }

    private Icon loadImage(String url) {
        URL imageUrl = null;
        Image image = null;

        try {
            imageUrl = new URL(SAMPLE_URL);
            try {
            image = Toolkit.getDefaultToolkit().createImage(imageUrl);
            } finally {
                return new ImageIcon(image);
            }
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
